#include "FiveWin.ch"

memvar oIde

//----------------------------------------------------------------------------//

CLASS TWndCode FROM TWindow

   DATA   oActiveEdit               // visible TextEdit control
   DATA   oRow, oCol
   DATA   oTabs, aSources
   DATA   oPopup

   CLASSDATA lRegistered AS LOGICAL

   METHOD New( oForm ) CONSTRUCTOR

   METHOD AddData( cDataName )

   METHOD AddPage( cPageName, cCode )
   
   METHOD BuildBar()

   METHOD Close() //INLINE ( ::bValid := nil, ::End() ) //daniel

   METHOD CloseActivePage()

   METHOD EditMethod( cMethodName, cClassName, aInfo )

   METHOD GotFocus()

   METHOD SaveAll()
   
   METHOD SetOption( nOption, nOldOption )

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( oForm ) CLASS TWndCode

   local hWndTaskBar, lTaskBarVisible

   DEFINE ICON ::oIcon RESOURCE "FiveWin"

   DEFINE FONT ::oFont NAME "Courier New" SIZE 0, -14

   Super:New( 7, 28, 46, 128, If( oForm != nil, "Source editor: " + oForm:cPrgName, "" ),,,, ::oIcon )

   hWndTaskBar = FindWindow( "Shell_TrayWnd", "" )
   if hWndTaskBar != 0
      lTaskBarVisible = WndTop( hWndTaskBar ) <= GetSysMetrics( 1 ) - WndHeight( hWndTaskBar )
   else
      lTaskBarVisible = .F.
   endif      
   ::nHeight = GetSysMetrics( 1 ) - oIde:oWndMain:nHeight - ;
               If( lTaskBarVisible, WndHeight( hWndTaskBar ), 0 )

   ::nWidth = GetSysMetrics( 0 ) - oIde:oInspector:nWidth //daniel


   ::aSources   = {} 
   ::bLostFocus = { || oIDE:oWndActive := Self }

   MENU ::oPopup POPUP 2007
      MENUITEM "&Close this file" RESOURCE "close" ACTION ::CloseActivePage()
      SEPARATOR
      MENUITEM "Select &All" ACTION ::oActiveEdit:SelectAll()
   ENDMENU

   ::BuildBar()

   @ 4, 0 TABS ::oTabs OF Self ;
      ON CHANGE ( ::SetOption( nOption, nOldOption ),;
                  ::SetText( "Source editor: " + ::oActiveEdit:cFileName ) )

   ::oTabs:aPrompts = {} 
   ::oBottom = ::oTabs

   // ::AddPage( "NoName", "" ) 

   /*
   SET MESSAGE OF Self TO "(c) FiveTech Software 2008" NOINSET 2007

   DEFINE MSGITEM ::oRow OF ::oMsgBar ;
      PROMPT "Row: 1" SIZE 70 ACTION ::oClient:oClient:DlgGoLine() ;
      TOOLTIP "click to goto line"

   DEFINE MSGITEM ::oCol OF ::oMsgBar PROMPT "Col: 1" SIZE 70
   */

   ::Show()

   // We don't let this window be destroyed. We just hide it!
   ::bValid = { || ::Hide(), .f. }

return Self

//----------------------------------------------------------------------------//

METHOD AddData( cDataName ) CLASS TWndCode

   local nAt, nNext

   if ( nAt := ::oActiveEdit:Search( "DATA" ) ) != 0
      while ( nNext := ::oActiveEdit:Search( "DATA", nAt + 1 ) ) != 0
         nAt = nNext
      end
      ::oActiveEdit:InsLine( nAt + 1, "   DATA   " + cDataName )
      ::oActiveEdit:Refresh()
   else
      // MsgInfo( "va a buscar CLASS desde code.prg" )
      nAt = ::oActiveEdit:Search( "CLASS" )
      ::oActiveEdit:Goto( nAt + 2 )
      // ::oActiveEdit:ReplaceSel( .t., "   DATA   " + cDataName )
      ::oActiveEdit:InsLine( nAt + 2, "   DATA   " + cDataName )
      ::oActiveEdit:InsLine( nAt + 3, "" )
   endif

   ::oActiveEdit:GoTop()

return nil

//----------------------------------------------------------------------------//

METHOD AddPage( cPageName, cCode ) CLASS TWndCode

   local oTxtEdit := TTxtEdit():New( 0, 0, 800, 400, Self )
   
   oTxtEdit:SetFont( ::oFont )
   oTxtEdit:SetText( cCode )
   oTxtEdit:cFileName = cPageName
   AAdd( ::aSources, oTxtEdit )
   ::oActiveEdit = oTxtEdit

   oTxtEdit:bChange = { || ::oRow:SetText( LTrim( Str( oTxtEdit:GetRow() + 1 ) ) ),;
   	                       ::oRow:Refresh(),;
                           ::oCol:SetText( LTrim( Str( oTxtEdit:GetCol() + 1 ) ) ),;
                           ::oCol:Refresh() }
   oTxtEdit:bLButtonUp = oTxtEdit:bChange
   oTxtEdit:bRClicked = { | nRow, nCol | ;
                          ::oPopup:Activate( nRow, nCol, oTxtEdit, .f. ) }

   ::oClient = oTxtEdit
   ::ReSize()
   
   AAdd( ::oTabs:aPrompts, Lower( cFileNoPath( cPageName ) ) )
   ::oTabs:SetTabs( ::oTabs:aPrompts )
   ::oTabs:SetOption( Len( ::oTabs:aPrompts ) )

   ::SetText( "Source editor: " + cPageName )
   ::oBar:AEvalWhen()

return oTxtEdit

//----------------------------------------------------------------------------//

METHOD BuildBar() CLASS TWndCode

   local oBar
   
   DEFINE BUTTONBAR oBar OF Self SIZE 30, 30 2007
   
   DEFINE BUTTON OF oBar RESOURCE "close" TOOLTIP "Close this file" ;
      MESSAGE "Close this file" ACTION ::CloseActivePage() WHEN Len( ::aSources ) > 1

   DEFINE BUTTON OF oBar GROUP RESOURCE "form" TOOLTIP "Show form" ;
      MESSAGE "Show this form" ACTION ( oIde:oActiveForm:Show(), oIde:oActiveForm:SetFocus() )

   DEFINE BUTTON OF oBar GROUP RESOURCE "gotop" TOOLTIP "Go Top" ;
      MESSAGE "Go to top of file" ACTION ( ::oActiveEdit:GoTop(), ::oActiveEdit:SetFocus() )

   DEFINE BUTTON OF oBar RESOURCE "gobottom" TOOLTIP "Go Bottom" ;
      MESSAGE "Go to bottom of file" ACTION ( ::oActiveEdit:GoBottom(), ::oActiveEdit:SetFocus() )

   DEFINE BUTTON OF oBar RESOURCE "goto" TOOLTIP "Go Line" ;
      MESSAGE "Go to line" ACTION ::oActiveEdit:DlgGoLine()

   DEFINE BUTTON OF oBar GROUP RESOURCE "find" TOOLTIP "Find" ;
      MESSAGE "Search text" ACTION ::oActiveEdit:DlgFind()

   DEFINE BUTTON OF oBar RESOURCE "findprev" TOOLTIP "Find prev" ;
      MESSAGE "Find previous ocurrence" ACTION ::oActiveEdit:FindPrev()

   DEFINE BUTTON OF oBar RESOURCE "findnext" TOOLTIP "Find next" ;
      MESSAGE "Find next ocurrence" ACTION ::oActiveEdit:FindNext()

   DEFINE BUTTON OF oBar GROUP RESOURCE "cut" TOOLTIP "Cut" ;
      MESSAGE "Cut selected text" ACTION ::oActiveEdit:Cut()

   DEFINE BUTTON OF oBar RESOURCE "copy" TOOLTIP "copy" ;
      MESSAGE "Copy selected text" ACTION ::oActiveEdit:Copy()

   DEFINE BUTTON OF oBar RESOURCE "paste" TOOLTIP "Paste" ;
      MESSAGE "Paste text" ACTION ::oActiveEdit:Paste()

   DEFINE BUTTON OF oBar RESOURCE "delete" TOOLTIP "Delete" ;
      MESSAGE "Delete selected text" ACTION ::oActiveEdit:Delete()

   DEFINE BUTTON OF oBar GROUP RESOURCE "print" TOOLTIP "Print" ;
      MESSAGE "Print text" ACTION ::oActiveEdit:Print()

   DEFINE BUTTON ::oRow OF oBar GROUP PROMPT "1" TOOLTIP "Row" ;
      MESSAGE "Current row of file" ACTION ( ::oActiveEdit:DlgGoLine() )

   DEFINE BUTTON ::oCol OF oBar GROUP PROMPT "1" TOOLTIP "Col" ;
      MESSAGE "Current col of file"

   DEFINE BUTTON OF oBar GROUP WHEN .F.
   
return nil   

//----------------------------------------------------------------------------//

METHOD EditMethod( cMethodName, cClassName, aInfo ) CLASS TWndCode

   local nAt, nNext, n
   local cParams := "(" + If( Len( aInfo ) > 1, " ", "" )

   for n = 2 to Len( aInfo )
      cParams += aInfo[ n ] + ", "
   next
   if Len( aInfo ) > 1
      cParams = SubStr( cParams, 1, Len( cParams ) - 2 ) + " "
   endif   
   cParams += ")"

   if ( nAt := ::oActiveEdit:Search( "METHOD " + cMethodName ) ) != 0
      ::oActiveEdit:Search( "METHOD " + cMethodName )
      ::oActiveEdit:SetFocus()
   else
      nAt = ::oActiveEdit:Search( "ENDCLASS" )
      ::oActiveEdit:InsLine( nAt, "   METHOD " + cMethodName + cParams )
      ::oActiveEdit:InsLine( nAt + 1 )
      ::oActiveEdit:AddLine()
      ::oActiveEdit:AddLine( "METHOD " + cMethodName + cParams + " CLASS " + cClassName )
      ::oActiveEdit:AddLine()
      ::oActiveEdit:AddLine( "   " )
      ::oActiveEdit:AddLine()
      ::oActiveEdit:AddLine( "return nil" )
      ::oActiveEdit:AddLine()
      ::oActiveEdit:AddLine( "//" + Replicate( "-", 72 ) + "//" )
      ::oActiveEdit:GoBottom()
   endif

   ::oActiveEdit:GoUp()
   ::oActiveEdit:GoUp()
   ::oActiveEdit:GoUp()
   ::oActiveEdit:GoNextChar()
   ::oActiveEdit:GoNextChar()
   ::oActiveEdit:GoNextChar()
   ::oActiveEdit:SetFocus()

return nil

//----------------------------------------------------------------------------//

METHOD CloseActivePage() CLASS TWndCode

   if ::oActiveEdit:lChanged
      if MsgYesNo( "Save changes ?",;
                   ::oActiveEdit:cFileName + " has changed" )
         ::oActiveEdit:SaveToFile()
      endif
   endif

   ::oActiveEdit:End()

		#ifdef __XHARBOUR__	//daniel
   		ADel( ::aSources, ::oTabs:nOption )
   	#else
 	 		ADel( ::aSources, ::oTabs:nOption )
 	 		ASize( ::aSources, len( ::aSources ) - 1 )
	  #endif
   
   ::oTabs:DelItem()
   if Len( ::aSources ) > 0 
      ::oActiveEdit = ::aSources[ ::oTabs:nOption ]
      ::SetText( "Source editor: " + ::oTabs:aPrompts[ ::oTabs:nOption ] ) //daniel
      ::oActiveEdit:Show()
   endif   

return nil

//----------------------------------------------------------------------------//

METHOD GotFocus() CLASS TWndCode

   if ::oActiveEdit != nil
      ::oActiveEdit:SetFocus()
   endif

return 0

//----------------------------------------------------------------------------//

METHOD SaveAll() CLASS TWndCode

   local n, oTxtEdit
   for n = 1 to Len( ::aSources )
      oTxtEdit = ::aSources[ n ]
      oTxtEdit:SaveToFile( alltrim( oIde:oProject:oProjectOpt:cPrjSource ) + "\" + cFileNoPath( oTxtEdit:cFileName ) )
   next

return nil

//----------------------------------------------------------------------------//

METHOD SetOption( nOption, nOldOption ) CLASS TWndCode

   if Len( ::aSources ) > 1 .and. nOption != nOldOption
      ::aSources[ nOldOption ]:Hide()
      ::oClient = ::oActiveEdit := ::aSources[ nOption ]
      ::aSources[ nOption ]:Show()
      ::ReSize()
   endif
   
return nil      

//----------------------------------------------------------------------------//


METHOD Close() CLASS TWndCode
	
	local n
		
   for n = 1 to Len( ::aSources )
      ::CloseActivePage()
   next
   
   ::bValid := nil
   ::end()

return nil